/**
 * (c)Copyright 2007 SinoCredit Co,Ltd. All Rights Reserved.  
 * PageBean.java, 6:58:41 PM 
 * @author Ming.Shi
 * @since Jun 27, 2007
 * @version 1.0
 */
package com.sys.plat.bean;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import com.sys.plat.util.HttpUtil;
import org.apache.commons.beanutils.BeanUtils;

@SuppressWarnings("unchecked")
public class PageBean {
    private String hql;
    private int pageno;
    private int totalrow;
    private int pagesize;
    private int pagestart;
    private Map filtermap;//<name,shiming;  >
    private String property;
    private String sortOrder;

    private List retList; //get form datebase

    public Map getFiltermap() {
        return filtermap;
    }

    public void setFiltermap(Map filtermap) {
        this.filtermap = filtermap;
    }

    public String getHql() {
        return hql;
    }

    public void setHql(String hql) {
        this.hql = hql;
    }

    public int getMaxpageno() {
        int size = getPagesize();
        return (totalrow / size) + (totalrow % size == 0 ? 0 : 1);
    }

    public int getPageno() {
        return pageno <= 0 ? 1 : pageno;
    }

    public void setPageno(int pageno) {
        this.pageno = pageno;
    }

    public int getPagesize() {
        return pagesize <= 0 ? 15 : pagesize;
    }

    public void setPagesize(int pagesize) {
        this.pagesize = pagesize;
    }

    public List getRetList() {
        return retList;
    }

    public void setRetList(List retList) {
        this.retList = retList;
    }

    public int getTotalrow() {
        return totalrow;
    }

    public void setTotalrow(int totalrow) {
        this.totalrow = totalrow;
    }

    public void setToWeb() {
        HttpUtil.reqSetAttr("pageBean", this);
    }

    public String getProperty() {
        return property;
    }

    public void setProperty(String property) {
        this.property = property;
    }

    public String getSortOrder() {
        return sortOrder;
    }

    public void setSortOrder(String sortOrder) {
        this.sortOrder = sortOrder;
    }

    public GftSort getGftSort() {
        return new GftSort(this.property, this.sortOrder);
    }

    public int getPagestart() {
        return pagestart;
    }

    public void setPagestart(int pagestart) {
        this.pagestart = pagestart;
    }


    @Deprecated
    public String setToJSON() {

        StringBuffer sb = new StringBuffer();

        sb.append("{totalProperty:").append(getTotalrow()).append(",");
        sb.append("root:[");

        for (int i = 0; i < getRetList().size(); i++) {
            try {
                Map map = BeanUtils.describe(getRetList().get(i));
                sb.append("{");

                Object[] key = map.keySet().toArray();
                for (int j = 0; j < key.length; j++) {
                    sb.append("'").append(key[j]).append("':");

                    String value = BeanUtils.getProperty(getRetList().get(i), String.valueOf(key[j]));
                    if (value == null || value.equals("null"))
                        value = "";

                    value = value.replaceAll("'", "\"");
                    sb.append("'").append(value).append("'");

                    if (j != key.length - 1) {
                        sb.append(",");
                    }
                }

                sb.append("}");
            } catch (IllegalAccessException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (InvocationTargetException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (NoSuchMethodException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
            if (i != getRetList().size() - 1) {
                sb.append(",");
            }
        }

        sb.append("]}");

        String r = sb.toString();

        r = r.replaceAll("\r\n", "");
        r = r.replaceAll("\t", "");
        r = r.replaceAll("\n", "");


        return r;
    }
}


